package com.cennavi.mineplay.mineplay_data.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

import org.apache.tomcat.util.http.fileupload.IOUtils;

import com.mercator.MercatorProjection;
import com.vividsolutions.jts.algorithm.distance.DistanceToPoint;
import com.vividsolutions.jts.algorithm.distance.PointPairDistance;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineString;

public class Tools {
	/*
	 * 解压缩数据 (Snappy)
	 */
	public static byte[] uncompress(byte[] arg0) {
	    ByteArrayOutputStream output = new ByteArrayOutputStream();
	    GZIPInputStream input = null;
	    try {
	        input = new GZIPInputStream(new ByteArrayInputStream(arg0));
	        IOUtils.copyLarge(input, output);
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    } finally {
	        IOUtils.closeQuietly(input);
	        IOUtils.closeQuietly(output);
	    }
	    return output.toByteArray();
	}
	public static Coordinate getVertical(LineString line, Coordinate coord) {
	      PointPairDistance distinct = new PointPairDistance();
	      DistanceToPoint.computeDistance(line, coord, distinct);
	      return distinct.getCoordinate(0);
	}
	// 角度计算
			public static double getDir(double x1, double y1, double x2, double y2) {
				double result = 0d;
				try {
					double resultX1 = MercatorProjection.longitudeToMetersX(x1);
					double resultY1 = MercatorProjection.latitudeToMetersY(y1);
					double resultX2 = MercatorProjection.longitudeToMetersX(x2);
					double resultY2 = MercatorProjection.latitudeToMetersY(y2);

					double distanceX = resultX2 - resultX1;
					double distanceY = resultY2 - resultY1;

					double cos = distanceX / Math.sqrt(Math.pow(distanceX, 2d) + Math.pow(distanceY, 2d));
					double angle = Math.acos(cos) * (180 / Math.PI);

					if (distanceY > 0) {
						result = -angle;
					} else if (distanceY < 0) {
						result = angle;
					} else if (distanceY == 0) {
						if (distanceX > 0) {
							result = 0;
						} else if (distanceX < 0) {
							result = 180;
						}
					}
				} catch (Exception e) {
					return result;
				}
				return result;
			}
}
